栈和队列的相互实现

用栈实现队列,用队列实现栈

Implement Stack using Queues

link
Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

题目大意就是说只能使用队列中的push,pop,front函数
解法一: 用两个队列实现q1, q2, q2中单独放栈顶的元素。push操作时,将push的数压入到q2中,并将q2中的元素加入到q1中,直到q2,只剩下一个元素。
对于top操作,判断q2是否为空,是空,就弹出q1的前面的元素,加入到q1的队尾(q1.size() -1个元素)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Stack {
public:
// Push element x onto stack.
void push(int x) {
q2.push(x);
while(q2.size() > 1){
q1.push(q2.front());
q2.pop();
}
}

// Removes the element on top of the stack.
void pop() {
top();
q2.pop();
}

// Get the top element.
int top() {
if(q2.empty()){
for(int i = 0; i < q1.size() -1; i++){
q1.push(q1.front());
q1.pop();
}
q2.push(q1.front());
q1.pop();
}
return q2.front();
}

// Return whether the stack is empty.
bool empty() {
return q2.empty() && q1.empty();
}
private:
queue<int> q1;
queue<int> q2;
};

解法二:在每次的push操作的时候,动点手脚,每次入栈时,都将所有的元素保存到一个临时队列中去,将新加入的元元素压入队列中,再将临时队列中所有的元素再拷回来。这样相当于逆序了。

演示过程: 1,3,5

  1. 队列:将1入栈
    q: 1
  2. 将3入栈
    弹出1, 压入3
    q:3
    在将临时队列的元素拷回来
    q:3 1(此时已经实现了倒序了)
  3. 将5入栈
    弹出3 1(队列的弹出), 压入5
    q:5
    将3 1拷回来
    q:5 3 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Stack {
public:
// Push element x onto stack.
void push(int x) {
queue<int> tmp;
while (!q.empty()) {
tmp.push(q.front());
q.pop();
}
q.push(x);
while (!tmp.empty()) {
q.push(tmp.front());
tmp.pop();
}
}

// Removes the element on top of the stack.
void pop() {
q.pop();
}

// Get the top element.
int top() {
return q.front();
}

// Return whether the stack is empty.
bool empty() {
return q.empty();
}
private:
queue<int> q;
};

Implement Queue using Stacks

Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

解题思路:
使用两个栈,st_in(用来接收数据),st_out(用于输出数据)。
push操作:直接将数据压入到st_in;
top操作:判断st_out是否为空,为空,就将st_in的所有数据压入到st_out中,取出栈顶的元素,不为空,直接取出
pop操作利用top操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
st_in.push(x);
}

// Removes the element from in front of queue.
void pop(void) {
peek();
st_out.pop();
}

// Get the front element.
int peek(void) {
if(st_out.empty()){
while(!st_in.empty()){
int num = st_in.top(); st_in.pop();
st_out.push(num);
}
}
return st_out.top();
}

// Return whether the queue is empty.
bool empty(void) {
return (st_in.empty()&&st_out.empty());
}

private:
stack<int> st_in;
stack<int> st_out;
};